#include "stdafx.h"
#include "TableHeaderView.h"

#include <QApplication>
#include <QMouseEvent>
#include <QPainter>

TableHeaderView::TableHeaderView( Qt::Orientation  orientation, QWidget * parent)
: QHeaderView(orientation, parent),
	m_bPressed(false),
	m_bChecked(false),
	m_bTristate(false),
	m_bNoChange(false),
	m_bMoving(false)
{
	setHighlightSections(false);
	setMouseTracking(true);

}


static QRect CheckBoxRect(const QStyleOptionViewItem &view_item_style_options)  
{  
	QStyleOptionButton check_box_style_option;  
	QRect check_box_rect = QApplication::style()->subElementRect(  
		QStyle::SE_CheckBoxIndicator,  
		&check_box_style_option);  

	QPoint check_box_point(view_item_style_options.rect.x() +  
		view_item_style_options.rect.width() / 2 -  
		check_box_rect.width() / 2,  
		view_item_style_options.rect.y() +  
		view_item_style_options.rect.height() / 2 -  
		check_box_rect.height() / 2);  
	return QRect(check_box_point, check_box_rect.size());  
}

TableHeaderView::~TableHeaderView()
{

}

void TableHeaderView::paintSection( QPainter *painter, const QRect &rect, int logicalIndex ) const
{
	painter->save();
	QHeaderView::paintSection(painter, rect, logicalIndex);
	painter->restore();

	if (logicalIndex == 0)
	{
		QStyleOptionButton option;
		option.initFrom(this);

		if (m_bChecked)
			option.state |= QStyle::State_Sunken;

		if (testAttribute(Qt::WA_Hover) && underMouse()) {
			if (m_bMoving)
				option.state |= QStyle::State_MouseOver;
			else
				option.state &= ~QStyle::State_MouseOver;
		}


		if (m_bTristate && m_bNoChange)
			option.state |= QStyle::State_NoChange;
		else
			option.state |= m_bChecked ? QStyle::State_On : QStyle::State_Off;
	
		
		QCheckBox checkBox;
		option.iconSize = QSize(20, 20);
		option.rect = rect;
		style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &option, painter, &checkBox);
		//style()->drawItemPixmap(painter, rect, Qt::AlignCenter, QPixmap(":/images/checkBoxChecked"));
		//style()->drawControl(QStyle::CE_CheckBox, &option, painter, m_pCheckBox);
	}
}

void TableHeaderView::mouseMoveEvent( QMouseEvent *e )
{
	int nColumn = logicalIndexAt(e->pos());
	if(nColumn == 0)
	{
		m_bMoving = true;
	}

	QHeaderView::mouseMoveEvent(e);
}

void TableHeaderView::mousePressEvent( QMouseEvent *e )
{
	int nColumn = logicalIndexAt(e->pos());
	if ((e->buttons() & Qt::LeftButton) && (nColumn == 0))
	{
		m_bPressed = true;
	}
	QHeaderView::mousePressEvent(e);
}

void TableHeaderView::mouseReleaseEvent( QMouseEvent *e )
{
	if (m_bPressed)
	{
		if (m_bTristate && m_bNoChange)
		{
			m_bChecked = true;
			m_bNoChange = false;
		}
		else
		{
			m_bChecked = !m_bChecked;
		}

		Qt::CheckState state = m_bChecked ? Qt::Checked : Qt::Unchecked;

		update();

		emit test();

		emit stateChanged(state);

		
	}

	m_bPressed = false;

	QHeaderView::mouseReleaseEvent(e);

	
}
//
//bool TableHeaderView::event(QEvent *event)
//{
//	if (event->type() == QEvent::Enter || event->type() == QEvent::Leave)
//	{
//		QMouseEvent *pEvent = static_cast<QMouseEvent *>(event);
//		int nColumn = logicalIndexAt(pEvent->x());
//		if (nColumn == 0)
//		{
//			m_bMoving = (event->type() == QEvent::Enter);
//
//			update();
//		}
//	}
//
//	return QHeaderView::event(event);
//}

void TableHeaderView::onStateChanged( int state )
{
	if (state == Qt::PartiallyChecked) {
		m_bTristate = true;
		m_bNoChange = true;
	} else {
		m_bNoChange = false;
	}

	m_bChecked = (state != Qt::Unchecked);
	update();
}
